package com.woniuxy.controller;

import com.woniuxy.ResponseResult;
import com.woniuxy.entity.Orders;
import com.woniuxy.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 *
 * @auth chenxuean
 * @date 2021/3/24
 * @time 16:33
 */

@RestController
@RequestMapping("orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     *  生成订单
     * @param ordermap
     * @return
     */
    @PostMapping("addOrders")
    public ResponseResult<Object> addOrders(Map<String,Object> ordermap){

        ordersService.addOrders(ordermap);

        return new ResponseResult<Object>(200,"添加订单成功");

    }

    /**
     * 删除订单(软删除)
     * @param ordersId
     * @return
     */
    @PostMapping("updateOrders")
    public ResponseResult<Object> updateOrders(String ordersId){

      int stu=  ordersService.updateOrders(ordersId);
        return  new ResponseResult<Object>(200,"修改删除状态成功",stu);
    }

    /**
     * 查询全部订单信息
     * @return
     */
    @PostMapping("queryAllOrders")
    public ResponseResult<Object> queryAllOrders(){

        List<Orders> qall=ordersService.queryAllOrders();

        return  new ResponseResult<>(200,"查询所有订单信息成功",qall);
    }

    /**
     * 根据订单号查询订单详情
     * @param ordersId
     * @return
     */
    @PostMapping("queryOrdersOne")
    public  ResponseResult<Orders> queryOrdersOne(String ordersId){
      Orders qoo= ordersService.queryOrdersOne(ordersId);

      return new ResponseResult<>(200,"根据订单号查询订单详情成功",qoo);
    }

    /**
     * 根据订单号修改配送状态
     * <p>
     * 0:未配送
     * 1:已接单
     * 2:取货中
     * 3:配送中
     * 4:已送达
     * 5:已超时
     *
     * @param ordersId
     * @return
     */
    @PostMapping("updateStatusOne")
    public ResponseResult<Object> updateDispatchingStatus(String ordersId,int state){
        if (state<6&&state>0){
            ordersService.updateDispatchingStatus(ordersId,state);
            if (state==1){
                return new ResponseResult<>(200," 根据订单号修改配送状态为 :已接单",state);
            }else if (state==2){
                return new ResponseResult<>(200," 根据订单号修改配送状态为 :取货中",state);
            }else if (state==3){
                return new ResponseResult<>(200," 根据订单号修改配送状态为 :配送中",state);
            }else if (state==4){
                return new ResponseResult<>(200," 根据订单号修改配送状态为 :已送达",state);
            }else if (state==5){
                return new ResponseResult<>(200," 根据订单号修改配送状态为 :已超时",state);
            }
        }
            return  new ResponseResult<>(500,"修改配送状态失败");

    }

    /**
     * 根据用户账号查询订单
     * @param account
     * @return
     */
    @PostMapping("findOrdersByUserAccount")
    public ResponseResult<Object> findOrdersByUserAccount(String account) {
        ordersService.findOrdersByUserAccount(account);

        return new ResponseResult<>(200,"根据用户账号查询订单 成功");
    }

    @GetMapping("sss")
    public String text(){
        return "sfdadf";
    }
}
